<template>
  <div class="common-layout">
    <el-container>
      <el-header>
        <span>username</span>
      </el-header>
      <el-container>
        <!-- 左边栏菜单 -->
        <el-aside width="200px">
          <el-menu
            class="el-menu-vertical-demo"
            default-active="2"
            active-text-color="#ffd04b"
            background-color="#545c64"
            text-color="#fff"
            @open="handleOpen"
            @close="handleClose"
            @select="handleLeftSelect"
          >
            <el-sub-menu
              v-for="item in menuTree"
              :index="item.path"
              :key="item.id"
            >
              <template #title>
                <el-icon><location /></el-icon>
                <span>{{ item.name }}</span>
              </template>
              <el-menu-item
                v-for="it in item.children"
                :index="it.path"
                :key="it.id"
                >{{ it.name }}</el-menu-item
              >
            </el-sub-menu>
          </el-menu>
        </el-aside>

        <!-- 右侧主页面 -->
        <el-main>
          <router-view />
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref } from "vue";
import {
  Document,
  Menu as IconMenu,
  Location,
  Setting,
} from "@element-plus/icons-vue";
import { useRouter } from "vue-router";
import { getMenuTree } from "../api/api";

const router = useRouter();

const activeIndex2 = ref("1");
const menuTree = ref([]);
const handleSelect = (key: string, keyPath: string[]) => {
  console.log(key, keyPath);
};

// 左侧菜单的点击事件
const handleLeftSelect = (key: string, keyPath: string[]) => {
  console.log(key, keyPath);
  router.push(key);
};

const handleOpen = (key: string, keyPath: string[]) => {
  //console.log(key, keyPath);
};
const handleClose = (key: string, keyPath: string[]) => {
  //console.log(key, keyPath);
};

// 页面加载钩子函数
onMounted(() => {
  getMenuTree().then((res: any) => {
    console.log(res);
    if (res.data.code == 200) {
      menuTree.value = res.data.data;
    }
  });
});
</script>

<style scoped></style>
